home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / utils / mmgr / aset.c next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  6.0 KB  |  338 lines

  1. /*
  2.  * aset.c --
  3.  *    Allocation set definitions.
  4.  *
  5.  * Note:
  6.  *    XXX This is a preliminary implementation which lacks fail-fast
  7.  *    XXX validity checking of arguments.
  8.  */
  9.  
  10. #include "tmp/c.h"
  11.  
  12. RcsId("$Header: /private/postgres/src/utils/mmgr/RCS/aset.c,v 1.11 1991/11/12 20:20:29 mer Exp $");
  13.  
  14. #include "utils/excid.h"    /* for ExhaustedMemory */
  15. #include "utils/memutils.h"
  16.  
  17. extern void bcopy();    /* XXX use header */
  18.  
  19. #undef AllocSetReset
  20. #undef malloc
  21. #undef free
  22.  
  23. /*
  24.  * Internal type definitions
  25.  */
  26.  
  27. /*
  28.  * AllocElem --
  29.  *    Allocation element.
  30.  */
  31. typedef struct AllocElemData {
  32.     OrderedElemData    elemData;    /* elem in AllocSet */
  33.     Size        size;
  34. } AllocElemData;
  35.  
  36. typedef AllocElemData *AllocElem;
  37.  
  38.  
  39. /*
  40.  * Private method definitions
  41.  */
  42.  
  43. /*
  44.  * AllocPointerGetAllocElem --
  45.  *    Returns allocation (internal) elem given (external) pointer.
  46.  */
  47. #define AllocPointerGetAllocElem(pointer)    (&((AllocElem)(pointer))[-1])
  48.  
  49. /*
  50.  * AllocElemGetAllocPointer --
  51.  *    Returns allocation (external) pointer given (internal) elem.
  52.  */
  53. #define AllocElemGetAllocPointer(alloc)    ((AllocPointer)&(alloc)[1])
  54.  
  55. /*
  56.  * AllocElemIsValid --
  57.  *    True iff alloc is valid.
  58.  */
  59. #define AllocElemIsValid(alloc)    PointerIsValid(alloc)
  60.  
  61. /*
  62.  * AllocSetGetFirst --
  63.  *    Returns "first" allocation pointer in a set.
  64.  *
  65.  * Note:
  66.  *    Assumes set is valid.
  67.  */
  68. static
  69. AllocPointer
  70. AllocSetGetFirst ARGS((
  71.     AllocSet    set
  72. ));
  73.  
  74. /*
  75.  * AllocPointerGetNext --
  76.  *    Returns "successor" allocation pointer.
  77.  *
  78.  * Note:
  79.  *    Assumes pointer is valid.
  80.  */
  81. static
  82. AllocPointer
  83. AllocPointerGetNext ARGS((
  84.     AllocPointer    pointer
  85. ));
  86.  
  87. /*
  88.  * Public routines
  89.  */
  90.  
  91. /* 
  92.  *    AllocPointerIsValid(pointer)
  93.  *     AllocSetIsValid(set)    
  94.  *
  95.  *        .. are now macros in aset.h -cim 4/27/91
  96.  */
  97.  
  98. void
  99. AllocSetInit(set, mode, limit)
  100.     AllocSet    set;
  101.     AllocMode    mode;
  102.     Size        limit;
  103. {
  104.     AssertArg(PointerIsValid(set));
  105.     AssertArg((int)DynamicAllocMode <= (int)mode);
  106.     AssertArg((int)mode <= (int)BoundedAllocMode);
  107.  
  108.     /*
  109.      * XXX mode is currently ignored and treated as DynamicAllocMode.
  110.      * XXX limit is also ignored.  This affects this whole file.
  111.      */
  112.  
  113.     OrderedSetInit(&set->setData, offsetof(AllocElemData, elemData));
  114. }
  115.  
  116. void
  117. AllocSetReset(set)
  118.     AllocSet    set;
  119. {
  120.     AllocPointer    pointer;
  121.  
  122.     AssertArg(AllocSetIsValid(set));
  123.  
  124.     while (AllocPointerIsValid(pointer = AllocSetGetFirst(set))) {
  125.         AllocSetFree(set, pointer);
  126.     }
  127. }
  128.  
  129. void
  130. AllocSetReset_debug(file, line, set)
  131.     String      file;
  132.     int            line;
  133.     AllocSet    set;
  134. {
  135.     AllocPointer    pointer;
  136.  
  137.     AssertArg(AllocSetIsValid(set));
  138.  
  139.     while (AllocPointerIsValid(pointer = AllocSetGetFirst(set))) {
  140.     alloc_set_message(file, line, pointer, set);
  141.     AllocSetFree(set, pointer);
  142.     }
  143. }
  144.  
  145. bool
  146. AllocSetContains(set, pointer)
  147.     AllocSet    set;
  148.     AllocPointer    pointer;
  149. {
  150.     AssertArg(AllocSetIsValid(set));
  151.     AssertArg(AllocPointerIsValid(pointer));
  152.  
  153.     return (OrderedSetContains(&set->setData,
  154.         &AllocPointerGetAllocElem(pointer)->elemData));
  155. }
  156.  
  157. AllocPointer
  158. AllocSetAlloc(set, size)
  159.     AllocSet    set;
  160.     Size        size;
  161. {
  162.     AllocElem    alloc;
  163.  
  164.     AssertArg(AllocSetIsValid(set));
  165.  
  166.     /* allocate */
  167.     alloc = (AllocElem)malloc(sizeof (*alloc) + size);
  168.     Trap(!PointerIsValid(alloc), ExhaustedMemory);
  169.  
  170.     /* add to allocation list */
  171.     OrderedElemPushInto(&alloc->elemData, &set->setData);
  172.  
  173.     /* set size */
  174.     alloc->size = size;
  175.  
  176.     return (AllocElemGetAllocPointer(alloc));
  177. }
  178.  
  179. void
  180. AllocSetFree(set, pointer)
  181.     AllocSet    set;
  182.     AllocPointer    pointer;
  183. {
  184.     AllocElem    alloc;
  185.  
  186.     /* AssertArg(AllocSetIsValid(set)); */
  187.     /* AssertArg(AllocPointerIsValid(pointer)); */
  188.     AssertArg(AllocSetContains(set, pointer));
  189.  
  190.     alloc = AllocPointerGetAllocElem(pointer);
  191.  
  192.     /* remove from allocation set */
  193.     OrderedElemPop(&alloc->elemData);
  194.  
  195.     /* free storage */
  196.     delete(alloc);
  197.     /* pg_free(alloc); */
  198. }
  199.  
  200. AllocPointer
  201. AllocSetRealloc(set, pointer, size)
  202.     AllocSet    set;
  203.     AllocPointer    pointer;
  204.     Size        size;
  205. {
  206.     AllocPointer    newPointer;
  207.     AllocElem    alloc;
  208.  
  209.     /* AssertArg(AllocSetIsValid(set)); */
  210.     /* AssertArg(AllocPointerIsValid(pointer)); */
  211.     AssertArg(AllocSetContains(set, pointer));
  212.  
  213.     /*
  214.      * Calling realloc(3) directly is not be possible (unless we use
  215.      * our own hacked version of malloc) since we must keep the
  216.      * allocations in the allocation set.
  217.      */
  218.  
  219.     alloc = AllocPointerGetAllocElem(pointer);
  220.  
  221.     /* allocate new pointer */
  222.     newPointer = AllocSetAlloc(set, size);
  223.  
  224.     /* fill new memory */
  225.     bcopy(pointer, newPointer, (alloc->size < size) ? alloc->size : size);
  226.  
  227.     /* free old pointer */
  228.     AllocSetFree(set, pointer);
  229.  
  230.     return (newPointer);
  231. }
  232.  
  233. Count
  234. AllocSetIterate(set, function)
  235.     AllocSet    set;
  236.     void        (*function) ARGS((AllocPointer pointer));
  237. {
  238.     Count        count = 0;
  239.     AllocPointer    pointer;
  240.  
  241.     AssertArg(AllocSetIsValid(set));
  242.  
  243.     for (pointer = AllocSetGetFirst(set);
  244.             AllocPointerIsValid(pointer);
  245.             pointer = AllocPointerGetNext(pointer)) {
  246.  
  247.         if (PointerIsValid(function)) {
  248.             (*function)(pointer);
  249.         }
  250.         count += 1;
  251.     }
  252.  
  253.     return (count);
  254. }
  255.  
  256. Count
  257. AllocSetCount(set)
  258. AllocSet set;
  259. {
  260.     Count        count = 0;
  261.     AllocPointer    pointer;
  262.  
  263.     AssertArg(AllocSetIsValid(set));
  264.  
  265.     for (pointer = AllocSetGetFirst(set);
  266.             AllocPointerIsValid(pointer);
  267.             pointer = AllocPointerGetNext(pointer)) {
  268.         count++;
  269.     }
  270.     return count;
  271. }
  272.  
  273. /*
  274.  * Private routines
  275.  */
  276.  
  277. static
  278. AllocPointer
  279. AllocSetGetFirst(set)
  280.     AllocSet    set;
  281. {
  282.     AllocElem    alloc;
  283.  
  284.     alloc = (AllocElem)OrderedSetGetHead(&set->setData);
  285.  
  286.     if (!AllocElemIsValid(alloc)) {
  287.         return (NULL);
  288.     }
  289.  
  290.     return (AllocElemGetAllocPointer(alloc));
  291. }
  292.  
  293. static
  294. AllocPointer
  295. AllocPointerGetNext(pointer)
  296.     AllocPointer    pointer;
  297. {
  298.     AllocElem    alloc;
  299.  
  300.     alloc = (AllocElem)OrderedElemGetSuccessor(
  301.         &AllocPointerGetAllocElem(pointer)->elemData);
  302.  
  303.     if (!AllocElemIsValid(alloc)) {
  304.         return (NULL);
  305.     }
  306.  
  307.     return (AllocElemGetAllocPointer(alloc));
  308. }
  309.  
  310.  
  311. /*
  312.  * Debugging routines
  313.  */
  314.  
  315. /*
  316.  * XXX AllocPointerDump --
  317.  *    Displays allocated pointer.
  318.  */
  319. void
  320. AllocPointerDump(pointer)
  321.     AllocPointer    pointer;
  322. {
  323.     printf("\t%-10d@ %0#x\n", ((long*)pointer)[-1], pointer); /* XXX */
  324. }
  325.  
  326. /*
  327.  * AllocSetDump --
  328.  *    Displays allocated set.
  329.  */
  330. void
  331. AllocSetDump(set)
  332.     AllocSet    set;
  333. {
  334.     int count;
  335.     count = AllocSetIterate(set, AllocPointerDump);
  336.     printf("\ttotal %d allocations\n", count);
  337. }
  338.